<?php
class FileSystemAction {

	public static function cleanup($string){
		return preg_replace('/\x2f+/','/',preg_replace('/[^\w\x2f\x2e]*/i','','/'.$string));
	}
	
	public static function get_subitems($filters,$target){
	
		$target=FileSystemAction::cleanup($target);
		global $Config;
		$fullpath=$Config->FileSystem->Root.$target;
		//build correct filters 
		$pat[0]='/\x2e+/';$pat[1]='/\?/';$pat[2]='/\*+/';$pat[3]='/;+/';$pat[4]='/^;|;$/';
		$rep[0]='\.';$rep[1]='.';$rep[2]='.*';$rep[3]=';';$rep[4]='';
		$filters=preg_split('/;/',preg_replace($pat,$rep,$filters));
				
		if( file_exists($fullpath) ) {
			$files = scandir($fullpath);
			natcasesort($files);
			echo '([';
			//alien code detected
			if( count($files) > 2 ) { /* The 2 accounts for . and .. */
				$first = true;
				foreach( $files as $file ) {
					$found=false;
					foreach ($filters as $pat) {
						if (preg_match('/'.$pat.'/i',$file)>0) $found=true;
					};
					if( file_exists($fullpath.$file) && $file != '.' && $file != '..' && $found) {
						if ($first == true) $first = false; else echo ',';
						$items = new stdClass();
						$item->n = $file;
						if (is_dir($fullpath.$file)){
							$item->p = htmlentities($target.$file).'/';
							$item->t = 'd';
							$item->s = -1;
						}else{
							$item->p = htmlentities($target.$file);
							$item->t = 'f';
							$item->s = filesize($fullpath.$file);
						}				
						echo json_encode($item);
					}
				}
			} //alien code end
			echo '])';
		}else{
			EchoError('NOT_FOUND_PATH',"File '$target' not found");
		}
	}
	
	public static function mkdir($dir){
	
		global $Config;
		$dir=preg_split('/\x2f/',FileSystemAction::cleanup($dir));
		unset($dir[0]);
		$deep='/';
		foreach ($dir as &$curr) {
			$deep.=$curr.'/';
			if (file_exists($Config->FileSystem->Root.$deep)) {
				ResultOK();
				continue;
			} else {
				if (mkdir($Config->FileSystem->Root.$deep)) {
					ResultOK();
				} else {
					EchoError('ERROR_CREATE_DIR',"directory '$deep' cannot be created!");
				}
			}
		}
	}
	
	public static function rmdir($dir){
	 
		global $Config;
		$dir=FileSystemAction::cleanup($dir.'/');
		if ($handle = opendir($Config->FileSystem->Root.$dir)) { 		
    		while ($obj = readdir($handle)) {
       			if ($obj != '.' && $obj != '..') { 
          			if (is_dir($Config->FileSystem->Root.$dir.$obj)) { 
           	   			self::rmdir($dir.$obj);
           			} elseif (is_file($Config->FileSystem->Root.$dir.$obj)) { 
						if (!unlink($Config->FileSystem->Root.$dir.$obj)) EchoError('CANNOT_DELETE_FILE',$dir.$obj);
           				} 
       			} 
    		} 
   			closedir($handle); 
   			if (rmdir($Config->FileSystem->Root.$dir)) {
   				ResultOK();
   			}
		} else EchoError('NO_SUCH_DIR',$target); 
	}
	
	public static function md5($target){
	
		global $Config;
		$target=$Config->FileSystem->Root.FileSystemAction::cleanup($target);
		if( file_exists($target) ) {
			echo '({md5:"'.md5_file($target).'"})';
		}else {
			EchoError('FILE_NOT_FOUND',"File '$target' not found"); 
		}
	}
	
	public static function get_content($target){
	
		global $Config;
		$target=FileSystemAction::cleanup($Config->FileSystem->Root.'/'.$target);
		if( file_exists($target) ) {
			readfile($target);
		}else {
			EchoError('FILE_NOT_FOUND',"File '$target' not found"); 
		}
	}
}
?>